﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ComplexNumber
{
    class ComplexNumber
    {
        private double real, imaginary;

        public double Imaginary
        {
            get { return imaginary; }
            set { imaginary = value; }
        }

        public double Real
        {
            get { return real; }
            set { real = value; }
        }

        public ComplexNumber()
        {
            real = 0;
            imaginary = 0;
        }

        public ComplexNumber(double r, double i)
        {
            real = r;
            imaginary = i;
        }

        public ComplexNumber(double r)
        {
            real = r;
            imaginary = 0;
        }

        public static ComplexNumber operator +(ComplexNumber summant1, ComplexNumber summant2)
        {
            return new ComplexNumber(summant1.Real + summant2.Real,
                                    summant1.imaginary + summant2.Imaginary);
        }

        public static ComplexNumber operator -(ComplexNumber summant1)
        {
            return new ComplexNumber(-summant1.Real, -summant1.Imaginary);
        }

        public static ComplexNumber operator -(ComplexNumber summant1, ComplexNumber summant2)
        {
            return summant1 + (-summant2);
        }

        public static ComplexNumber operator *(ComplexNumber multiplier1, ComplexNumber multiplier2)
        {
            return new ComplexNumber(multiplier1.Real * multiplier2.Real - multiplier1.Imaginary * multiplier2.Imaginary,
                                        multiplier1.Real * multiplier2.Imaginary + multiplier1.Imaginary * multiplier2.Real);
        }

        public static ComplexNumber operator /(ComplexNumber nominator, ComplexNumber denominator)
        {
            return new ComplexNumber(nominator.Real * denominator.Real + nominator.Imaginary * denominator.Imaginary /
                                        ((denominator.Real * denominator.Real) + (denominator.Imaginary * denominator.Imaginary)),
                                    nominator.Real * denominator.Imaginary - nominator.Imaginary * denominator.Real /
                                        ((denominator.Real * denominator.Real) + (denominator.Imaginary * denominator.Imaginary))
                                        );
        }

        public static bool operator ==(ComplexNumber compared1, ComplexNumber compared2)
        {
            return compared1.Real == compared2.Real && compared1.Imaginary == compared2.Imaginary;
        }

        public static bool operator !=(ComplexNumber compared1, ComplexNumber compared2)
        {
            return !(compared1 == compared2);
        }

        public override string ToString()
        {
            return Real.ToString() + " + " + Imaginary.ToString() + "i";
        }

        public static implicit operator ComplexNumber(double r)
        {
            return new ComplexNumber(r);
        }

        public static implicit operator double(ComplexNumber complex)
        {
            if (complex.Imaginary != 0) return double.NaN;
            else return complex.Real;
        }

        public double this[int index]
        {
            get
            {
                if (index > 1 || index < 0) throw new IndexOutOfRangeException();
                return (index == 0) ? real : imaginary;
            }
            set
            {
                if (index > 1 || index < 0) throw new IndexOutOfRangeException();
                if (index == 0) real = value;
                else imaginary = value;
            }
        }


    }

    class Program
    {
        static void Main(string[] args)
        {
            ComplexNumber c1 = new ComplexNumber(1, 1);
            ComplexNumber c2 = 2;
            double d1 = c1;
            double d2 = c2;
            Console.WriteLine(d1.ToString());
            Console.WriteLine(d2.ToString());
            Console.WriteLine((c1 + c2).ToString());
            Console.WriteLine((c1 - c2).ToString());
            Console.WriteLine((c1 * c2).ToString());
            Console.WriteLine((c1 / c2).ToString());
            Console.WriteLine(c2[1]);
            Console.WriteLine(c2[-1]);
        }
    }
}
